package example.com.zyfypt_618zgm.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.util.SortedList;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.activity.Denglu;
import example.com.zyfypt_618zgm.activity.UserMain;
import example.com.zyfypt_618zgm.bean.FocusResult;
import example.com.zyfypt_618zgm.bean.UserBean;
import example.com.zyfypt_618zgm.model.DelFocusModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 小茗同学 on 2017/6/1.
 */

public class FocusAdapter extends RecyclerView.Adapter{

    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    public List<FocusResult<UserBean>> list;//保存要显示的数据
    public static int id;

    public FocusAdapter(Context context) {
        this.context=context;
        layoutInflater= LayoutInflater.from(context);
    }
    public void setList(List<FocusResult<UserBean>> list){
        this.list=list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_username,tv_realname,tv_rolename;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tv_username = (TextView) itemView.findViewById(R.id.textView);
            tv_realname = (TextView) itemView.findViewById(R.id.textView2);
            tv_rolename = (TextView) itemView.findViewById(R.id.textView3);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item, parent, false);
        return new FocusAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
        FocusResult<UserBean> userBean = list.get(position);
        if (userBean == null)
            return;
        ViewHolder viewholder = (ViewHolder) holder;
        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"+userBean.getBean().getUsername()).placeholder(R.drawable.touxiang).into(viewholder.imageView);
        viewholder.tv_username.setText(userBean.getBean().getUsername());
        viewholder.tv_realname.setText(userBean.getBean().getRealname());
        viewholder.tv_rolename.setText(userBean.getBean().getRolename());
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                id=list.get(position).getBean().getId();
                Intent intent=new Intent();
                intent.setClass(context,UserMain.class);
                context.startActivity(intent);
            }
        });
        viewholder.itemView.setOnLongClickListener(new View.OnLongClickListener(){
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示");
                builder.setMessage("确认取消关注吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DelFocusModel delFocusModel = new DelFocusModel();
                        Call<String> call= delFocusModel.getResult("userfocus",list.get(position).getBean().getUserid(), Denglu.SessionID);
                        call.enqueue(new Callback<String>() {
                            @Override
                            public void onResponse(Call<String> call, Response<String> response) {
                                if(response.isSuccessful()&& response.body() != null){
                                    if(response.body().equals("1")){
                                        Toast.makeText(context,"取消关注成功",Toast.LENGTH_SHORT).show();//取消关注成功
                                    }
                                    else {
                                        Toast.makeText(context,"取消关注失败",Toast.LENGTH_SHORT).show();//取消关注失败
                                    }
                                }
                            }
                            @Override
                            public void onFailure(Call<String> call, Throwable t) {
                                Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
                            }
                        });
                        list.remove(position);
                        setList(list);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                return false;
            }
        });
    }


    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        return list.size();
    }
}
